package com.usian.article.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.article.service.impl.AuthorServiceImpl;
import com.usian.apis.article.AuthorControllerApi;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/author")
public class AuthorController implements AuthorControllerApi {

    @Autowired
    AuthorServiceImpl authorService;

    @GetMapping("/findByUserId/{id}")
    @Override
    public ApAuthor findByUserId(@PathVariable("id") Integer id) {
        //返回对象,id相等的
        return authorService.getOne(Wrappers.<ApAuthor>lambdaQuery().eq(id!=null,ApAuthor::getUserId,id));
    }
    @PostMapping("/save")
    @Override
    public ResponseResult save(@RequestBody ApAuthor apAuthor) {
        //为空返回
        if(apAuthor==null) return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        //添加
        authorService.save(apAuthor);
        return ResponseResult.okResult(apAuthor);
    }

    @GetMapping("/getByAuthorId")
    public ApAuthor findByid(Integer id){
        return authorService.getById(id);
    }
}
